import { createStore } from 'vuex'
import { localStorageSet, localStorageGet, localStorageRemove } from '../utils/function'

const store = createStore({
  state: {
    sea_amoy_admin_token: '',
    sea_amoy_admin_userInfo: {}
  },
  mutations: {
    //登录
    login(state, payload) {
      state.sea_amoy_admin_token = payload.token
      state.sea_amoy_admin_userInfo = payload
      localStorageSet('sea_amoy_admin_token', payload.token)
      localStorageSet('sea_amoy_admin_userInfo', payload)
    },
    //退出登录
    logout(state) {
      state.sea_amoy_admin_token = ''
      state.sea_amoy_admin_userInfo = {}
      localStorageRemove('sea_amoy_admin_token')
      localStorageRemove('sea_amoy_admin_userInfo')
    },
    //重载信息
    loadInfo(state) {
      const token = localStorageGet('sea_amoy_admin_token')
      const userInfo = localStorageGet('sea_amoy_admin_userInfo')
      if (token) {
        state.sea_amoy_admin_token = token
        state.sea_amoy_admin_userInfo = userInfo
      }
    }
  },
  actions: {},
  getters: {}
})

export default store
